<?php

/**
 * This is the model class for table "lths_depart".
 *
 * The followings are the available columns in table 'lths_depart':
 * @property string $depart_code
 * @property string $depart_name
 * @property string $eparchy_code
 * @property string $remark
 * @property string $create_time
 * @property string $update_time
 */
class Depart extends ParamModel
{
	
	public $keyname='depart_code';
	public $displayname='depart_name';
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Depart the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	
	

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'lths_depart';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('depart_code', 'length', 'max'=>20),
			array('depart_name', 'length', 'max'=>50),
			array('eparchy_code', 'length', 'max'=>4),
			array('remark', 'length', 'max'=>200),
			array('create_time, update_time', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('depart_code, depart_name, eparchy_code, remark, create_time, update_time', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'depart_code' => 'Depart Code',
			'depart_name' => 'Depart Name',
			'eparchy_code' => 'Eparchy Code',
			'remark' => 'Remark',
			'create_time' => 'Create Time',
			'update_time' => 'Update Time',
		);
	}
	/**
	 * 根据地市编码获取营业厅列表
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function getListDataByEparchy($eparchy_code)
	{
		$results = array();
		$models = $this->findAllByAttributes(array('eparchy_code'=>$eparchy_code));
	    foreach ($models as $key => $value) {
	    	$results[$value[$this->keyname]] = $value[$this->displayname];
	    }
	    return $results;
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('depart_code',$this->depart_code,true);
		$criteria->compare('depart_name',$this->depart_name,true);
		$criteria->compare('eparchy_code',$this->eparchy_code,true);
		$criteria->compare('remark',$this->remark,true);
		$criteria->compare('create_time',$this->create_time,true);
		$criteria->compare('update_time',$this->update_time,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	function getEparchyJson()
	{
		$results = array();
		$models = $this->findAll();
		foreach($models as $model)
		{
			if(!isset($results[$model->eparchy_code]))
			{
				$results[$model->eparchy_code]=array();				
			}
			$results[$model->eparchy_code][]=array($model->depart_code=>$model->depart_name);
		}
		return CJSON::encode($results);
	}
}